package leetcode.top100;

import org.junit.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：zsy
 * @date ：Created 2022/10/18 11:19 AM
 * @description：
 */
public class _003_无重复字符的最长子串 {
    @Test
    public void test() {
        System.out.println(new Solution().lengthOfLongestSubstring("pwwkew"));
    }

    class Solution {
        public int lengthOfLongestSubstring(String s) {
            int maxLen = 0;
            Map<Character, Integer> map = new HashMap<>();
            int l = 0;
            int r = 0;
            for (; r < s.length(); r++) {
                char ch = s.charAt(r);
                if (map.containsKey(ch)) {
                    maxLen = Math.max(maxLen, r - l);
                    l = Math.max(l, map.get(ch));
                }
                map.put(ch, r + 1);
            }
            return Math.max(maxLen, r - l);
        }
    }
}
